package cz.zcu.fav.rat.managed.bean.calendar;

import java.util.Date;
import java.util.List;

import org.joda.time.LocalDateTime;
import org.primefaces.model.LazyScheduleModel;

import cz.zcu.fav.rat.beans.Period;
import cz.zcu.fav.rat.dataaccess.service.PeriodService;
import cz.zcu.fav.rat.managed.bean.SpringBeanFactory;
import cz.zcu.fav.rat.managed.bean.calendar.event.IEventFactory;

public class CalendarModelEventUser extends LazyScheduleModel
{
	private static final long serialVersionUID = 2289536988195388006L;

	private transient PeriodService periodService;
	private Integer id;
	private IEventFactory factory;
	public CalendarModelEventUser(Integer id, IEventFactory factory)
	{
		this.id = id;
		this.factory = factory;
	}

	public Integer getId()
	{
		return id;
	}

	public void loadEvents(Date start, Date end)
	{
		LocalDateTime since = new LocalDateTime(start);
		LocalDateTime to = new LocalDateTime(end);

			List<Period> periods = loadData(since, to);

			for (Period period : periods)
			{
				ReservationEvent event = factory.getEvent(period);
				addEvent(event);
			}
	}


	protected List<Period> loadData(LocalDateTime since, LocalDateTime to)
	{
		List<Period> periods = getPeriodService()
				.getAllPeriodsByUserIdInDateRange(getId(), since, to);
		return periods;
	}

	PeriodService getPeriodService()
	{
		if (periodService == null)
		{
			periodService = SpringBeanFactory.getInstance().getBean(
					"periodService", PeriodService.class);
		}
		return periodService;
	}
}
